	function confirmDelete(act)
	{
		var agree=confirm("Are you sure you want to " + act + "?");
		if (agree)
			return true ;
		else
			return false ;
	}


  function checkDate() {
	// Check we're not backwards with our dates
        if (document.getElementById('startdate').value > document.getElementById('enddate').value)
        {
		$("div#warningtext").text("End date must be after the start date!");
		$("div#daycount").text("");
		document.getElementById('submit').disabled = true;
        }
        else
        {
		if ($("#startdate").val().substring(0,4) != $("#enddate").val().substring(0,4))
		{
			$("div#warningtext").text("Start and end dates cannot be in different years!");
			$("div#daycount").text("");
			document.getElementById('submit').disabled = true;
		}
		else
		{

	                $("div#warningtext").text(" ");
	                document.getElementById('submit').disabled = false;
					// Do the AJAX to find out how many days this will use
	                $.ajax({
	                        url:"daysCalculation.php",
	                        data:"sd=" + $("#startdate").val() + "&st=" + $("#starttime").val() + "&ed=" + $("#enddate").val() + "&et=" + $("#endtime").val(),
	                        success: function(data) {
					
					//Work out if we have enough days for selected request - two periods, this yr and next
					if ($("#startdate").val().substring(0,4) == (new Date).getFullYear())
					{
						if ($("#thisRemain").val() < parseFloat(data)) // Not enough days this year
						{
							$("div#warningtext").html("Not enough holiday days remaining.<br>You need "+ data + " days, but only have " + $("#thisRemain").val());
							$("div#daycount").text("");
				                        document.getElementById('submit').disabled = true;
						}
						else
							$("div#daycount").text("Days required for request: " + data);
					}
					else
					{
						if ($("#nextRemain").val() < parseFloat(data)) // Not enough days next year
						{
							$("div#warningtext").text("Not enough holiday days remaining. You need "+ data + " days, but only have " + $("#nextRemain").val());
							$("div#daycount").text("");
                                                        document.getElementById('submit').disabled = true;
						}
						else
							$("div#daycount").text("Days required for request: " + data);
					}
	                        }
	                });




	        }
	}
  }

$(document).ready(
  function()
  {

    $("#startdatepicker").datepicker(
    {
      onSelect: function(dateText, inst) {
	document.getElementById('startdate').value=dateText;
	checkDate();
      },
      onChangeMonthYear: function(year, month, inst){
	$("#enddatepicker").datepicker("setDate", year + '-' + month + '-01');
      },
      dateFormat: "yy-mm-dd",
      maxDate: new Date(2013, 12 - 1, 31),
      minDate: new Date(2012, 1 - 1, 1)
    }
    );

    $("#enddatepicker").datepicker(
    {
      onSelect: function(dateText, inst) {
        document.getElementById('enddate').value=dateText;
		checkDate();
      },
      dateFormat: "yy-mm-dd",
      maxDate: new Date(2013, 12 - 1, 31),
      minDate: new Date(2012, 1 - 1, 1)
    }
    );

    $("#starttime").change(function () {
      checkDate();
    });

    $("#endtime").change(function () {
      checkDate();
    });

	// Force checkdate on main.php pageload
	if ($("#thisRemain").val() != null)
		checkDate();
	
	
  }
);


